package com.yunzhou.interceptor;

import com.yunzhou.constants.RedisConstants;
import com.yunzhou.mapper.UserMapper;
import com.yunzhou.properties.JwtProperties;
import com.yunzhou.utils.JwtUtil;
import com.yunzhou.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

@Component
public class JwtInterceptor implements HandlerInterceptor {

    /**
     * 前置拦截，令牌验证，在Controller执行之前
     * 做登录验证校验，只需要判断ThreadLocal中的用户信息是否存在即可，存在说明已登录放行，不存在说明未登录进行未授权拦截。
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 没有用户信息，未登录，设置状态码401
        if (ThreadLocalUtil.getCurrentUserId() == null) {
            // 出错时返回的状态码为401
            response.setStatus(401);
            // 拦截
            return false;
        }
        // ThreadLocal中已有用户信息，已登录，放行
        return true;
    }

}
